package com.smart.primary.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import com.smart.primary.common.redis.JedisUtils;
import com.smart.primary.common.util.StringUtil;


/**
* @ClassName: ContextHolderUtils 
* @Description: TODO(上下文工具类) 
* @author  张代浩 
* @date 2012-12-15 下午11:27:39 
*
 */
public class ContextHolderUtils {
	public static String deviceToken = "deviceToken";
	/**
	 * SpringMvc下获取request
	 * 
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request;

	}
	/**
	 * SpringMvc下获取response
	 * 
	 * @return
	 */
	public static HttpServletResponse getResponse() {
		return null;

	}
	public static void setCacheSession(String sessionId,Map<String, Object> sessionMap) {
		JedisUtils.setObjectMap(sessionId, sessionMap, 3600);
	}
	 
	 
	
	public static String getSessionId() {
		HttpServletRequest request = getRequest();
		return ServletUtil.getMaidouSessionId(request, getResponse());
	}
	public static String getDeviceToken(HttpServletRequest request) {
		String deviceToken = request.getParameter(ContextHolderUtils.deviceToken);
		if(StringUtil.isNotEmpty(deviceToken) && !"null".equalsIgnoreCase(deviceToken)) {
			return deviceToken;
		}
		
		deviceToken = ContextHolderUtils.getCookieValue(ContextHolderUtils.deviceToken);
		if(deviceToken != null && StringUtil.isNotEmpty(deviceToken) && !"null".equalsIgnoreCase(deviceToken)) {
			return deviceToken;
		}
		return "";
	}
	
	public static void putSession(String key,Object value) {
		ServletUtil.putSession(ContextHolderUtils.getRequest(), ContextHolderUtils.getResponse(), key, value);
//		if(StringUtil.isNotEmpty(value) && StringUtil.isNotEmpty(key)) {
//			Map<String, Object> sessionMap = ContextHolderUtils.getCacheSession();
//			sessionMap.put(key, value);
//			String sessionId = ContextHolderUtils.getSessionId();
//			ContextHolderUtils.setCacheSession(sessionId, sessionMap);
//		}
	}
	public static String getSessionValue(String key) {
		Object obj = ServletUtil.getSession(ContextHolderUtils.getRequest(), ContextHolderUtils.getResponse(), key);
//		Map<String, Object> sessionMap = ContextHolderUtils.getCacheSession();
//		Object obj = sessionMap.get(key);
		if(obj != null) {
			return obj+"";
		}
		return "";
	}
	
	public static Object getSessionObjectValue(String key) {
		Object obj = ServletUtil.getSession(ContextHolderUtils.getRequest(), ContextHolderUtils.getResponse(), key);
//		Map<String, Object> sessionMap = ContextHolderUtils.getCacheSession();
//		Object obj = sessionMap.get(key);
		if(obj != null) {
			return obj;
		}
		return null;
	}

	
	private static final Map<String, HttpSession> sessionMap = new HashMap<String, HttpSession>();
	
	public static HttpSession getSession(){
		HttpServletRequest request = getRequest();
		return request.getSession();
	}
	public static void removeSessionValue(String key){
		ServletUtil.removeSession(ContextHolderUtils.getRequest(), ContextHolderUtils.getResponse(), key);
//		Map<String, Object> sessionMap = ContextHolderUtils.getCacheSession();
//		sessionMap.remove(key);
//		String sessionId = ContextHolderUtils.getSessionId();
//		ContextHolderUtils.setCacheSession(sessionId, sessionMap);
	}
	public static void removeSession(String sessionId){
		ServletUtil.removeSession(getRequest(), getResponse(), sessionId);
	}

	public static String getCookieValue(String name) {
		HttpServletRequest request = getRequest();
		Cookie[] coo = request.getCookies();
		if(coo!=null) {
			for (int i = 0; i < coo.length; i++) {
				Cookie cookie = coo[i];
				if(name.equalsIgnoreCase(cookie.getName())) {
					return cookie.getValue();
				}
			}
		}
		return null;
	}
	
	public static void setDeviceToken(HttpServletRequest request,HttpServletResponse response) {
		if(!StringUtil.hasWeb(request)) {
			String	deviceToken = ContextHolderUtils.getCookieValue(ContextHolderUtils.deviceToken);
			if(StringUtil.isNotEmpty(deviceToken) && !"null".equalsIgnoreCase(deviceToken)) {
				Cookie cookie = new Cookie(ContextHolderUtils.deviceToken, deviceToken);
				//设置cookie有效期为一个月
				cookie.setMaxAge(3600*24*30);
				response.addCookie(cookie);
				request.setAttribute(ContextHolderUtils.deviceToken, deviceToken);
				ContextHolderUtils.putSession(ContextHolderUtils.deviceToken, deviceToken);
			}
		}
	}

}
